package com.cy.ppj.web.front.order;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.order.OrderAddAO;
import com.cy.ppj.model.domian.user.UserInfo;
import com.cy.ppj.model.dto.order.OrderAddDTO;
import com.cy.ppj.model.dto.customer.CustomerOrderDTO;
import com.cy.ppj.model.dto.user.UserLoginDTO;
import com.cy.ppj.model.enums.OrderStatus;
import com.cy.ppj.model.query.OrderQuery;
import com.cy.ppj.model.vo.OrderDetailVO;
import com.cy.ppj.model.vo.OrderVO;
import com.cy.ppj.service.account.UserInfoService;
import com.cy.ppj.service.order.OrderService;
import com.cy.ppj.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 前台-订单模块
 * @author: tanyilin
 * @create: 2018年7月23日 上午9:49:36
 */
@Slf4j
@RestController
@RequestMapping("/user/order")
@Api(tags = "前台-订单模块")
public class OrderController extends BaseController {

	@Autowired
	private OrderService orderService;
	@Autowired
    private UserInfoService userInfoService;
	
	@ApiOperation("添加订单")
	@PostMapping("/add")
	public Result add(@Validated OrderAddAO orderAddAO, BindingResult bindingResult) {
		this.checkError(bindingResult);
		UserInfo userInfo = new UserInfo();
        userInfo.setUserId(this.getUser().getUserId());
        userInfo = userInfoService.queryUserInfo(userInfo);
        state(userInfo != null, "经纪人请先进行实名认证");
        // 校验 经纪人与客户身份证号码不可一致，即客户和经纪人不能是同一个人
        state(!orderAddAO.getIdNo().equals(userInfo.getIdNo()), "客户信息不能与经纪人雷同，请联系您的客户经理进件。");
		// 进行交单限制
		CustomerOrderDTO customerOrderDTO = orderService.selectCustomerOrderDTO(orderAddAO.getIdNo(), orderAddAO.getProductId());
		if (customerOrderDTO != null) {
			if(customerOrderDTO.getProcessingCount() > 0) {
				return ok("客户已有正在处理中的该产品申请，请勿重复提交");
			}
    		//最后一次审核通过时间与现在的相隔的天数
//    		int days = CommonUtils.diffDays(customerOrderDTO.getLastPassTime(), new Date());
//    		state(days > 30, "30天内不能重复申请");
    	}
		
		OrderAddDTO orderAddDTO = new OrderAddDTO();
		BeanUtils.copyProperties(orderAddAO, orderAddDTO);
		orderAddDTO.setUserId(this.getUser().getUserId());
		orderAddDTO.setOrderStatus(OrderStatus.APPLY_SUCCESS);
		orderService.addOrder(orderAddDTO);
		return ok();
	}
	
	@ApiOperation("前台订单列表")
	@GetMapping("/list")
	public PageBean<OrderVO> list(OrderQuery orderQuery) {
		UserLoginDTO user = this.getUser();
		orderQuery.setUserId(user.getUserId());
		orderQuery.setUserType(user.getType());
		PageBean<OrderVO> pageBean = orderService.queryOrderList(orderQuery);
		return pageBean;
	}
	
	@ApiOperation("前台订单详情")
	@GetMapping("/detail")
	public Result<OrderDetailVO> detail(@ApiParam(value = "订单id", example = "0", required = true) @RequestParam Long orderId) {
		OrderDetailVO orderDetailVO = orderService.orderDetail(orderId);
		return ok("", orderDetailVO);
	}
	
}
